package com.asset.salesforce.controller.pages;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.Role;
import com.asset.salesforce.domain.User;
import com.asset.salesforce.dto.roles.RolesDTO;
import com.asset.salesforce.service.EmployeeService;
import com.asset.salesforce.service.RoleShiroService;
import com.asset.salesforce.service.UserShiroService;

// TODO: Auto-generated Javadoc
/**
 * The Class ManageUsersController.
 */
@Controller
public class ManageUsersController {

	/** The employee service. */
	@Autowired
	private EmployeeService employeeService;
	
	/** The user shiro service. */
	@Autowired
	private UserShiroService userShiroService;
	
	/** The role shiro service. */
	@Autowired
	private RoleShiroService roleShiroService;
	
	/**
	 * Gets the accounts page.
	 *
	 * @param session the session
	 * @param model the model
	 * @param response the response
	 * @return the accounts page
	 */
	@RequestMapping(value = "/manageUsers", method = RequestMethod.GET)
	public String getAccountsPage(HttpSession session, Model model,HttpServletResponse response) {
		if (null == session.getAttribute("userDetails")) {return "redirect:login";}
		if (SecurityUtils.getSubject().hasRole("owner")){
			return "manageUsersPage";
		}
		else{
			return "accessDenied";
		}
	}
	
	/**
	 * Gets the roles model.
	 *
	 * @param session the session
	 * @param model the model
	 * @param response the response
	 * @param request the request
	 * @return the roles model
	 */
	@RequestMapping(value = "/rolesList", method = RequestMethod.GET)
	public String getRolesModel(HttpSession session, Model model,HttpServletResponse response,HttpServletRequest request) {
		if (null == session.getAttribute("userDetails")) {return "redirect:login";}
		if (SecurityUtils.getSubject().hasRole("owner")){
			Integer empId = Integer.parseInt(request.getParameter("empId"));
			Employee employee = employeeService.getById(empId);
			User user = userShiroService.findUser(employee.getEmail());
			List<String>userRoles = new ArrayList<String>();
			if (user!=null) {
				for (Role role : user.getRoles()) {
					userRoles.add(role.getName());
				}
			}
			model.addAttribute("empId",empId);
			model.addAttribute("rolesDTO", new RolesDTO());
			model.addAttribute("userRoles",userRoles);
			return "rolesListPage";
		}
		else{
			return "accessDenied";
		}
	}
	
	/**
	 * Post roles model.
	 *
	 * @param model the model
	 * @param session the session
	 * @param rolesDTO the roles dto
	 * @param result the result
	 * @return the string
	 */
	@RequestMapping(value = "/rolesList", method = RequestMethod.POST)
	public String postRolesModel(Model model,HttpSession session,@Valid @ModelAttribute("rolesDTO") RolesDTO rolesDTO,BindingResult result) {
		if (null == session.getAttribute("userDetails")) {return "redirect:login";}
		List<Role>roles = new ArrayList<Role>();
		if(rolesDTO.getOwner()!=null&&!rolesDTO.getOwner().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("owner"));
		}
		if(rolesDTO.getLeadgenerator()!=null&&!rolesDTO.getLeadgenerator().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("leadgenerator"));
		}
		if(rolesDTO.getSalesmanager()!=null&&!rolesDTO.getSalesmanager().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("salesmanager"));
		}
		if(rolesDTO.getSalesman()!=null&&!rolesDTO.getSalesman().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("salesman"));
		}
		if(rolesDTO.getBussinessdeveloper()!=null&&!rolesDTO.getBussinessdeveloper().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("bussinessdeveloper"));
		}
		if(rolesDTO.getLeadAdd()!=null&&!rolesDTO.getLeadAdd().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("leadAdd"));
		}
		if(rolesDTO.getLeadEdit()!=null&&!rolesDTO.getLeadEdit().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("leadEdit"));
		}
		if(rolesDTO.getLeadView()!=null&&!rolesDTO.getLeadView().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("leadView"));
		}
		if(rolesDTO.getLeadDelete()!=null&&!rolesDTO.getLeadDelete().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("leadDelete"));
		}
		if(rolesDTO.getLeadFellow()!=null&&!rolesDTO.getLeadFellow().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("leadFellow"));
		}
		if(rolesDTO.getLeadConvert()!=null&&!rolesDTO.getLeadConvert().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("leadConvert"));
		}
		if(rolesDTO.getLeadImport()!=null&&!rolesDTO.getLeadImport().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("leadImport"));
		}
		if(rolesDTO.getAccountAdd()!=null&&!rolesDTO.getAccountAdd().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("accountAdd"));
		}
		if(rolesDTO.getAccountEdit()!=null&&!rolesDTO.getAccountEdit().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("accountEdit"));
		}
		if(rolesDTO.getAccountView()!=null&&!rolesDTO.getAccountView().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("accountView"));
		}
		if(rolesDTO.getAccountDelete()!=null&&!rolesDTO.getAccountDelete().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("accountDelete"));
		}
		if(rolesDTO.getAccountFellow()!=null&&!rolesDTO.getAccountFellow().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("accountFellow"));
		}
		if(rolesDTO.getContactAdd()!=null&&!rolesDTO.getContactAdd().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("contactAdd"));
		}
		if(rolesDTO.getContactEdit()!=null&&!rolesDTO.getContactEdit().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("contactEdit"));
		}
		if(rolesDTO.getContactView()!=null&&!rolesDTO.getContactView().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("contactView"));
		}
		if(rolesDTO.getContactDelete()!=null&&!rolesDTO.getContactDelete().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("contactDelete"));
		}
		if(rolesDTO.getContactFellow()!=null&&!rolesDTO.getContactFellow().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("contactFellow"));
		}
		
		if(rolesDTO.getCallAdd()!=null&&!rolesDTO.getCallAdd().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("callAdd"));
		}
		if(rolesDTO.getCallEdit()!=null&&!rolesDTO.getCallEdit().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("callEdit"));
		}
		if(rolesDTO.getCallView()!=null&&!rolesDTO.getCallView().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("callView"));
		}
		if(rolesDTO.getAppointmentAdd()!=null&&!rolesDTO.getAppointmentAdd().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("appointmentAdd"));
		}
		if(rolesDTO.getAppointmentEdit()!=null&&!rolesDTO.getAppointmentEdit().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("appointmentEdit"));
		}
		if(rolesDTO.getAppointmentView()!=null&&!rolesDTO.getAppointmentView().equalsIgnoreCase("")){
			roles.add(roleShiroService.findRole("appointmentView"));
		}
		Employee employee = employeeService.getById(rolesDTO.getEmpId());
		User user = userShiroService.findUser(employee.getEmail());
		if(user!=null){
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		return "manageUsersPage";
	}
}
